/**
 * 定义多个可以间接更改state数据的方法  本质核心是通过commit 去触发mutations里面的方法
 * 
 */

// export const RECEIVE_GOODS = 'receive_goods' // 接收商品数组
// export const RECEIVE_RATINGS = 'receive_ratings' // 接收商家评价数组
// export const RECEIVE_INFO = 'receive_info' // 接收商家信息


// export const INCREMENT_FOOD_COUNT = 'increment_food_count' // 增加food 的count
// export const DECREMENT_FOOD_COUNT = 'decrement_food_count' // 减少food 的count

import Vue from 'vue'


import {
    RECEIVE_ADDRESS, RECEIVE_CATEGORYS, RECEIVE_SHOPS,
    RECEIVE_USER_INFO,
    RESET_USER_INFO,
    RECEIVE_GOODS,
    RECEIVE_RATINGS,
    RECEIVE_INFO,
    INCREMENT_FOOD_COUNT,
    DECREMENT_FOOD_COUNT,
    CLEAR_CART
} from './mutation-types'

import {
    reqAddress, reqCategorys, reqShops, reqUser, reqLogout,

    reqShopInfo, reqShopRatings, reqShopGoods

} from '../api/index'

export default {
    //5k
    async getAddress(context) {
        let geohash = context.state.latitude + ',' + context.state.longitude
        const result = await reqAddress(geohash)
        if (result.code === 0) {
            let obj = { address: result.data }

            context.commit(RECEIVE_ADDRESS, obj)

        }
    },


    async getCategorys(context) {
        const result = await reqCategorys()
        if (result.code === 0) {
            let obj = { categorys: result.data }



            context.commit(RECEIVE_CATEGORYS, obj)


        }

    },



    async getShops(context) {
        let aaa = { latitude: context.state.latitude, longitude: context.state.longitude }
        const result = await reqShops(aaa)
        if (result.code === 0) {
            let obj = { shops: result.data }
            context.commit(RECEIVE_SHOPS, obj)
        }

    },






    // async   getAddress({commit,state}) {

    //     let geohash=state.latitude+','+state.longitude
    //     const result=    await reqAddress(geohash)
    //     if(result.code===0)  commit(RECEIVE_ADDRESS,{address:result.data})

    // },



    //   async  getCategorys({commit}) {
    //     const result=    await reqCategorys()
    //     if(result.code===0)  commit(RECEIVE_CATEGORYS,{categorys:result.data})



    // },

    // async  getShops({commit,state}) {
    //     let{latitude,longitude}={state}
    //     const result=  await reqShops({latitude,longitude})
    //     if(result.code===0)  commit(RECEIVE_SHOPS,{shops:result.data})

    // }




    // 异步获取用户信息

    async getUserInfo({ commit }) {

        let result = await reqUser()
        console.log(result)
        if (result.code === 0) {
            commit(RECEIVE_USER_INFO, { userInfo: result.data })
        }

    },



    //记录用户信息
    recordUserInfo({ commit }, { userInfo }) {

        commit(RECEIVE_USER_INFO, { userInfo })
        window.localStorage.setItem('userinfo', JSON.stringify(userInfo))

    },

    //退出登录
    async logout({ commit }, callback) {
        let res = await reqLogout()

        if (res.code == 0) {

            commit(RESET_USER_INFO)
            callback && callback()
        }
    },


    //获取商家信息 
    async getShopInfo({ commit }) {
        let res = await reqShopInfo()
        if (res.code == 0) {
            commit(RECEIVE_INFO, { info: res.data })


        }
    },

    //获取商品信息
    async getShopGoods({ commit }, callback) {
        let res = await reqShopGoods()
        console.log('goods')
        if (res.code == 0) {
            commit(RECEIVE_GOODS, { goods: res.data })
            callback && callback()


        }
    },

    //获取评分组件信息
    async getShopRatings({ commit }) {
        let res = await reqShopRatings()
        if (res.code == 0) {
            commit(RECEIVE_RATINGS, { ratings: res.data })


        }
    },



    //购车车 方法 触发  
    updateFoodCount({ commit, state }, { isAdd, food }) {


       


        if (isAdd) {  //如果为true 表示数量加加  
            if (!food.count) {
                //  food.count=1   data 更新了 但是视图不会更新 
                Vue.set(food, 'count', 1)

                commit(INCREMENT_FOOD_COUNT, { food })

            } else {
                food.count++
            }



        } else {    // 表示商品数量 减减

            if (food.count) {  // 至少数量大于1 

                food.count--;


                commit(DECREMENT_FOOD_COUNT, { food })

            }

        }


    },


    //清空购物车 
    clearCart({commit}) {
        /**
         * 可能
         * 
         */

        commit(CLEAR_CART)
    }
        


     



}


